from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger

def paginate(users, users_per_page, page_number):
	"""
	Takes a group of users, a number of users_per_page, and the current page number. 
	Returns a Paginator object of the variables.
	"""
	paginator = Paginator(users, users_per_page);
	try:
		pageinated_users = paginator.page(page_number)
	except PageNotAnInteger:
		# If page is not an integer, deliver first page.
		pageinated_users = paginator.page(1)
	except EmptyPage:
		# If page is out of range, deliver last page of results.
		pageinated_users = paginator.page(paginator.num_pages)

	return pageinated_users

def calcualte_score(n, d):
    """
    Calcualtes a percentage (integer in the range 0-100) from n divided by d, 
    the percentage is rounded up to closest integer.
    If n is not >=0 and d is not >0, 0 is returned.
    """
    if d > 0 and n >=0:
        result_float = (n / float(d)) * 100
        result_int = int(result_float + 0.5)
        return result_int
    else:
        return 0